import { getTypeof, isEmpty, treeToList } from 'src/utils/utils'

/**
 * 表格 树形表头转线形
 */
export function treeConvertList (align = 'left', columns = [], paramIndex = 0) {
  const resultList = [], LENGTH = columns.length
  for (let i = 0; i < LENGTH; i++) {
    if (columns[i].hide && !columns[i].export) continue
    const column = columns[i]
    column.required = column.showColumn
    if (!['index', 'menu'].includes(column.name)) column.name = `${column.field}--${paramIndex}-${i}`
    column.align = column.align || align
    column.style = column.style || ''
    column.headerStyle = column.headerStyle || ''
    if (column.exportWidth) column.exportWidth = Number(column.exportWidth.replace(/(px|%)/g, ''))
    if (column.width) {
      const NUM = isNaN(Number(column.width)) ? column.width : `${column.width}px`
      column.style += `width: ${NUM}; `
      column.headerStyle += `width: ${NUM}; `
      if (!column.exportWidth) column.exportWidth = Number(NUM.replace(/(px|%)/g, ''))
      delete column.width
    }
    if (column.minWidth) {
      const NUM = isNaN(Number(column.minWidth)) ? column.minWidth : `${column.minWidth}px`
      column.style += `min-width: ${NUM};`
      column.headerStyle += `min-width: ${NUM};`
      if (!column.exportWidth) column.exportWidth = Number(NUM.replace(/(px|%)/g, ''))
      delete column.minWidth
    }
    if (column.maxWidth) {
      const NUM = isNaN(Number(column.maxWidth)) ? column.maxWidth : `${column.maxWidth}px`
      column.style += `max-width: ${NUM};`
      column.headerStyle += `max-width: ${NUM};`
      if (!column.exportWidth) column.exportWidth = Number(NUM.replace(/(px|%)/g, ''))
      delete column.maxWidth
    }
    if (!column.children?.length) resultList.push(column)
    else resultList.push.apply(resultList, treeConvertList(align, column.children, paramIndex + 1))
  }
  return resultList
}

/**
 * 获得表格表头合并规则
 */
export function convertToRows (originColumns = []) {
  let maxLevel = 1
  const traverse = (column, parent) => {
    if (parent) {
      column.level = parent.level + 1
      if (maxLevel < column.level) maxLevel = column.level
    }
    if (column.children) {
      let colSpan = 0
      for (const subColumn of column.children) {
        if (subColumn.hide) continue
        traverse(subColumn, column)
        colSpan += subColumn.colSpan
      }
      column.colSpan = colSpan
    } else column.colSpan = isEmpty(column.colSpan) ? 1 : column.colSpan
  }
  for (const column of originColumns) {
    if (column.hide) continue
    column.level = 1
    traverse(column)
  }
  const rows = []
  for (let i = 0; i < maxLevel; i++) rows.push([])
  const columns = treeToList(originColumns)
  columns.forEach(column => {
    if (column.children) column.rowSpan = 1
    else column.rowSpan = maxLevel - column.level + 1
    rows[column.level - 1].push(column)
  })
  return rows
}

/**
 * options中根据value获得label
 */
export function getLabel (column = {}) {
  if (isEmpty(column.value)) return ''
  if (getTypeof(column.options) !== 'array') return column.value
  const props = column.props || {}, label = (props.label || 'label').split(',')[0]
  if (column.multiple) {
    const values = column.value.split(',')
    let list = column.options.filter(el => {
      return getTypeof(el) === 'object' ? values.includes(el[props.value || 'value']) : values.includes(el)
    })
    list = list.map(el => getTypeof(el) === 'object' ? el[label] : el)
    return list.join(', ') || column.value
  }
  const object = column.options.find(el => {
    return getTypeof(el) === 'object' ? column.value === el[props.value || 'value'] : column.value === el
  })
  return getTypeof(object) === 'object' ? object[label] : object || column.value
}

/**
 * 判断行是否可被选择
 */
export function selectable (func = '', row = {}) {
  return typeof func === 'boolean' ? func : typeof func !== 'function' ? true : func(row)
}

/**
 * 获取方法返回值或字符串本身
 */
export const getResult = (func = '', props = {}) => typeof func === 'function' ? func(props) : func